package com.rzico.quartz.job;

import com.rzico.base.CommResult;
import com.rzico.core.entity.SysJob;
import com.rzico.core.service.SysJobService;
import com.rzico.core.service.SysPvuvSummaryService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.order.entity.Askfor;
import com.rzico.order.entity.Order;
import com.rzico.order.enumx.AskforStatusEnum;
import com.rzico.order.enumx.OrderShippingStatusEnum;
import com.rzico.order.service.AskforService;
import com.rzico.order.service.OrderService;
import com.rzico.util.RedisHandler;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongzm on 2020/2/2.
 * 计划任务执行demo
 */
public class OrderCompleted implements Job {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysPvuvSummaryService sysPvuvSummaryService;

    @Autowired
    protected SysJobService sysJobService;

    @Autowired
    protected AskforService askforService;

    @Autowired
    protected SysSequenceService sysSequenceService;

    @Autowired
    protected OrderService orderService;

    @Autowired
    private RedisHandler redisHandler;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String jobId = jobExecutionContext.getTrigger().getKey().getName();
        SysJob sysJob = sysJobService.selectByPrimaryKey(jobId);
        try {

            if (sysJob.getExecStatus().equals(1)) {
                //不能重复执行
                return;
            }
            sysJob.setExecStatus(1);
            sysJob.setJobDesc("开始执行:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);

            // 这里开始查询30分钟前未付款的订单，自动关闭, 20分钟定时查一次
            //   TODO

            Map<String,Object> params = new HashMap<>();
            params.put("orderStatus",1);
            params.put("shippingStatus", OrderShippingStatusEnum.SHIPPING_STATUS_SHIPPNG.getId());
            params.put("estimateBeforeDate", DateUtils.addDays(new Date(),-7));
            List<Order> orderList = orderService.selectList(params);

            for (Order order:orderList) {
                try {
                    //检查存在未完成退款申请，不签收

                    Map<String,Object> chkParams = new HashMap<>();
                    chkParams.put("orderId",order.getId());

                    List<Askfor> askforList = askforService.selectList(chkParams);
                    for (Askfor rn:askforList) {
                        if (!rn.getStatus().equals(AskforStatusEnum.ASKFOR_STATUS_COMPLETE.getId())
                                &&
                            !rn.getStatus().equals(AskforStatusEnum.ASKFOR_STATUS_CANCLEED.getId())
                           ) {
                            continue;
                        }
                    }

                    orderService.received(order.getId(),true);

                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }
            // end

            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行成功:"+sdf.format(new Date())+",下次执行:"+sdf.format(jobExecutionContext.getNextFireTime()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }
        catch (Exception e) {
            sysJob.setExecStatus(0);
            sysJob.setJobDesc("执行错误:"+sdf.format(new Date()));
            sysJobService.updateByPrimaryKeySelective(sysJob);
        }

    }
}
